/******************************************************************************жа
 * @brief: modbus app
 * @Author: aple
 * @Date: 2022-12-14 08:40:58
 * @LastEditors: aple
 * @LastEditTime: 2022-12-14 14:24:05
 * @Verision: 1.0.0.0
 ******************************************************************************/

#define _MAPP_MODULE_
#include "mbapp.h"
#include "sys_init.h"
#include "framework.h"
#include "bsp_api.h"
#include "dataproc.h"
#include "sys_task.h"

#define TEST_MODE MB_RTU_SLAVE

mb_baud_tab_create;

uint8_t mbTestErrCnt = 0;
uint8_t mbTestOkCnt = 0;

typedef struct
{
	uint16_t reg_r[64];
	uint16_t reg_w[64];
	uint16_t pv_r[64];
	uint16_t pv_w[64];
	uint16_t obj_size;
} mb_tst_type;

mb_tst_type mb_tst;

void rcv_end_handle_comp(uint8_t ErrSt);
void qc_task(const void *argv);
void modbus_task(const void *argv);
void qc_mb_data_handle(void);
void gui_app_task(void);

/******************************************************************************
 * @brief   modbus task creat.
 * @param   argv
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
obj_init_app(mb_task_creat);

void mb_task_creat(void)
{
	osThreadDef(modbus_task, osPriorityNormal, 1, 0x400);
	os_obj.tid_mb_task = osThreadCreate(osThread(modbus_task), NULL);
}

/******************************************************************************
 * @brief   modbus task.
 * @param   argv
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void modbus_task(const void *argv)
{
	qc_task(argv);
}

void rcv_end_handle_comp(uint8_t ErrSt){};

/******************************************************************************
 * @brief   mb handle.
 * @param   none
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void qc_callback(uu8 site, uu8 err)
{
	if (err)
	{
		mb.err_val = err;
	}
}
void qc_task(const void *argv)
{
	qc_cmd_type mcmd;
	uu8 bdone;

	//qc02_Init(QC_MODE_SLAVE, 115200, MB_PAR_NONE);
	//mb.qc02.os_event_send = mb_os_send;
	
	// config
	qc01_Init(QC_MODE_SLAVE, 115200, MB_PAR_NONE);
	mb.qc01.os_event_send = mb_os_send;

	// cmd config
	mcmd.id = 1;
	mcmd.wdat = mb_tst.pv_w;
	mcmd.rdat = mb_tst.pv_r;
	mcmd.wa = word_make(0xF, 5);
	mcmd.wn = 4;
	mcmd.ra = word_make(0x00, 0x10);
	mcmd.rn = 6;
	mcmd.callback = qc_callback;
	mcmd.attr = QC_MB_ATTR_HOLD_RW;
	mqc_stc_cmd_req(&mb.qc01, 0, &mcmd);

	mcmd.wa = word_make(0x0C, 8);
	mcmd.wn = 8;
	mcmd.ra = word_make(0x0C, 8);
	mcmd.rn = 8;
	mcmd.wdat = mb_tst.reg_w;
	mcmd.rdat = mb_tst.reg_r;
	mcmd.attr = QC_MB_ATTR_HOLD_RW;
	mb.qc01.fun->stc_cmd_req(2, &mcmd);

	// task handle
	for (;;)
	{
		osEvent event;

		// wait modbus event
		event = osSignalWait(0, 1000);

		(void)event;

		qc_mb_data_handle();
		
		qc_mb_poll(&mb.qc01);
		
		//qc_mb_poll(&mb.qc02);
	}
}

/******************************************************************************
 * @brief   mb handle.
 * @param   none
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void qc_mb_data_handle(void)
{
	
}
